<?php

/**
 * admincustomers actions.
 *
 * @package    retailbrain
 * @subpackage admincustomers
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class admincustomersActions extends sfActions {

    /**
     * Executes customers action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Clientes');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Clientes", "admincustomers/index"));
        
        $q = Doctrine_Query::create()
                ->from('Customers c')
                ->orderBy('c.name desc');
        
        $this->customers = $q->fetchArray();
        
        $this->caps = $this->getUser()->getAttribute('caps');
        
    }

    
    /**
     * Executes editCustomers action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditCustomers(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Cliente');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Cliente", "admincustomers/index"), array("Editar Cliente", "admincustomers/editCustomers?id=$id"));
        
        
        /** Customers **/
        $q = Doctrine_Query::create()
                ->from('Customers c')
                ->where('c.id_customer = ?', array($id))
                ->fetchArray();

        $this->customer = $q;
        
    }
    
    
    /**
     * Executes addCustomers action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddCustomers(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Cliente');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Clientes", "admincustomers/index"), array("Agregar Cliente", "admincustomers/addCustomers"));
        
    }
    
    
    /**
     * Executes createCustomer action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateCustomer(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('customers');
        
        try {

            $tabla = Doctrine::getTable('Customers');
            $tabla = new Customers();

            $tabla->name        = strtoupper($_arrayPost['name']);
            
            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/admincustomers/index');
    }
    
    
    /**
     * Executes updateCustomer action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateCustomer(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('customers');
        
        try {

            $tabla = Doctrine::getTable('Customers')->find(array($_arrayPost["id"]));

            $tabla->name        = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/admincustomers/index');
    }
    
    
    
    /**
     * CustomerActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveCustomer(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Customers c')
                ->set('c.active', '?', $active)
                ->where('c.id_customer = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Customers c')
                ->orderBy('c.name desc');
        
        $this->customers = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstCustomers');
    }
    
    /**
     * CustomerDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeleteCustomer(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Customers c')
                ->set('c.deleted', '?', $deleted)
                ->where('c.id_customer = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Customers c')
                ->orderBy('c.name desc');
        
        $this->customers = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');

        $this->setTemplate('lstCustomers');
    }
    
    
    /**
     * End Users 
     */
    

}
